using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ORS.OrderRobot.Domain;
using ORS.OrderRobot.Domain.Entity;

namespace ORS.OrderRobot.EFCore
{
    public class OrsDbContext : DbContext
    {
        public OrsDbContext(DbContextOptions<OrsDbContext> options) : base(options)
        {
        }

        public DbSet<Admin> Admins { get; set; }
        public DbSet<Account> Accounts { get; set; }
        public DbSet<AccountOrder> AccountOrders { get; set; }
        public DbSet<AccountBoarding> AccountBoardings { get; set; }
        public DbSet<ProductMointerLog> ProductMointerLog { get; set; }
        public DbSet<SystemConfig> SystemConfigs { get; set; }
        public DbSet<FlightInfo> FlightInfos { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            modelBuilder.Entity<Admin>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Password).IsRequired().HasMaxLength(100);
                entity.HasIndex(u => u.Username).IsUnique();
            });

            modelBuilder.Entity<Account>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Password).IsRequired().HasMaxLength(100);
                entity.HasIndex(e => e.Email).IsUnique();
            });

            modelBuilder.Entity<AccountBoarding>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.Birthday).HasMaxLength(20);
                entity.Property(e => e.Phone_Mobile).HasMaxLength(20);
                entity.Property(e => e.First_Name).HasMaxLength(30);
                entity.Property(e => e.Last_Name).HasMaxLength(30);
                entity.Property(e => e.DepartureDate).HasMaxLength(20);
                entity.Property(e => e.DepartureTime).HasMaxLength(5);
                entity.Property(e => e.Airlines).HasMaxLength(20);
                entity.Property(e => e.AirlinesNo).HasMaxLength(20);
                entity.Property(e => e.FlightNo).HasMaxLength(20);
                entity.HasOne(e => e.Account)
                    .WithMany(a => a.Boardings)
                    .HasForeignKey(e => e.AccountId)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<AccountOrder>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.OrderNo).IsRequired().HasMaxLength(50);
                
                entity.HasOne(e => e.Account)
                    .WithMany(a => a.AccountOrders)
                    .HasForeignKey(e => e.AccountId);

                entity.HasOne(e => e.AccountBoarding)
                    .WithMany(b => b.Orders)
                    .HasForeignKey(e => e.BoardingId);
            });

            modelBuilder.Entity<ProductMointerLog>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.ProductName).IsRequired().HasMaxLength(100);
            });

            modelBuilder.Entity<SystemConfig>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
            });

            modelBuilder.Entity<FlightInfo>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.AirlineCode).IsRequired().HasMaxLength(5);
                entity.Property(e => e.AirlineName).IsRequired().HasMaxLength(50);
                entity.Property(e => e.FlightNo).IsRequired().HasMaxLength(10);
            });
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            // 禁用所有数据库命令日志（包括 SQL 和参数日志）
            optionsBuilder
                .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddFilter((category, level) =>
                    category != "Microsoft.EntityFrameworkCore.Database.Command")))
                .EnableSensitiveDataLogging(false);  // 禁用敏感数据日志（如参数值）
        }
    }
} 